package com.study.webapp.leetCode_init;

import com.alibaba.fastjson.JSON;

import java.util.Arrays;

/**
 * 删除排序数组中的重复项
 */
public class init_1 {

    public static void main(String[] args) {
        int[] a = {1,1,2,2};
        int b = removeDuplicates(a);
        System.out.println(b);
        System.out.println(JSON.toJSONString(a));
    }

    public static int removeDuplicates(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }

        int count = 1;
        int index = 1;
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] == nums[index - 1]) {
                continue;
            }
            nums[index] = nums[i];
            index++;
            count++;
        }
        return count;
    }
}
